#!/usr/bin/env node

const os = require("os");
const { config } = require("dotenv");
const nodemailer = require("nodemailer");

let ip =
  Object.values(os.networkInterfaces())
    .flat(2)
    .filter((d) => !d.internal)
    .map((d) => d.address)
    .find((s) => s.match(/^\d+\.\d+\.\d+\.\d+$/)) || "";

config({ override: true });
let { MAIL_USER, MAIL_PASS, MAIL_HOST, MAIL_TO_ME, USER, USERNAME } =
  process.env;

let transport = nodemailer.createTransport({
  host: MAIL_HOST,
  auth: {
    user: MAIL_USER,
    pass: MAIL_PASS,
  },
});

let mailOptions = {
  from: MAIL_USER + "@sina.com",
  to: MAIL_TO_ME,
  subject: "Raspberry Login",
  html: `<!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>boot info</title>
  </head>
  <body>
  <div><h3><span style="color: #333">${
    USER || USERNAME
  } : </span> <span style="color: green">${ip}</span></h3></div>
  </body>
  </html>`,
};

transport
  .sendMail(mailOptions)
  .then((info) => console.log("Mail is delevered success!"))
  .catch((error) => console.log(error.toString()));
